package com.zzl.myblog.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zzl.myblog.Bean.ViewBean.TagsSimpleBean;
import com.zzl.myblog.R;
import com.zzl.myblog.View.Activity.WebActivity;

import java.util.List;

// 导向碎片中 "知识体系" 与 "拓维导航" 标签适配器
// recycleView + cardView + flowLayout
public class TagsAdapter extends RecyclerView.Adapter<TagsAdapter.viewHolder> {

    String TAG ="TagsAdapter";

    //TagsAdapter的成员变量systemBeanList.DataBean, 这里被我们用作数据的来源
    private List<TagsSimpleBean> systemSimpleBeanList;

    private String username;

    private Context context;

    //构造函数
    public TagsAdapter(List<TagsSimpleBean> systemSimpleBeanList, String username) {

        this.systemSimpleBeanList = systemSimpleBeanList;

        this.username=username;
    }


    @NonNull
    @Override
    public TagsAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //获取context
        context = viewGroup.getContext();

        View itemView = LayoutInflater.from(context).
                inflate(R.layout.flow_tags_card_item, viewGroup, false);

        return new viewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull viewHolder viewHolder, int i) {
        //通过其get()方法可以获得其中的对象
        TagsSimpleBean systemSimpleBean = systemSimpleBeanList.get(i);

        // 标题
        viewHolder.tvTitle.setText(systemSimpleBean.getTitle());

        //获取布局填充器,一会将tv.xml文件填充到标签内.
        final LayoutInflater mInflater = LayoutInflater.from(context);

        /*
            流式布局：http://www.mamicode.com/info-detail-2307546.html
         */
        //流式布局，设置数据源
        viewHolder.tflTags.setAdapter(new TagAdapter<String>(systemSimpleBean.getTags()) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.flow_tv_item,
                        viewHolder.tflTags, false);
                tv.setText(s);
                return tv;
            }
        });

        //流式布局，设置点击事件
        viewHolder.tflTags.setOnTagClickListener((view, position, parent) -> {
            Toast.makeText(context, systemSimpleBean.getTags()[position], Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(context, WebActivity.class);
            intent.putExtra("webURL",systemSimpleBean.getWebURLs()[position]);//username
            intent.putExtra("username",username);
            context.startActivity(intent);

            return true;
        });

    }

    @Override
    public int getItemCount() {
        return systemSimpleBeanList.size();
    }

    public class viewHolder extends RecyclerView.ViewHolder {
        //d对应flow_tags_card_item.xml

        //体系标题
        TextView tvTitle;

        //流式布局 http://www.mamicode.com/info-detail-2307546.html
        TagFlowLayout tflTags;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_flow_tags_card_title);
            tflTags = itemView.findViewById(R.id.id_flow_layout);
        }
    }
}
